/**
 *  *2010-11-12 下午08:36:55  add 
 */
package com.base.myproject.client.ui.celleditor;

import com.extjs.gxt.ui.client.core.El;
import com.extjs.gxt.ui.client.core.XDOM;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.widget.form.NumberPropertyEditor;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;

/**
 * 0表示什么都没有
 * 1是第一个2第二3第三个
 * @author Administrator
 *2010-11-12
 * javazgw@gmail.com
 */
public class BaseRadioGroup extends BaseCheckGroup  {
	static int count = 0;
	
	public BaseRadioGroup(String[] title){
		super(title);
	}
	
	public BaseRadioGroup(String[] title,boolean newline){
		super(title,newline);
	}
	protected void init(){
		this.setHeight(title.length*20+"px");
		input = new El[this.checknum ];
		for(int i = 0; i<this.checknum ;i++){
			input[i] = new El(DOM.createInputRadio(title[i]+count++));
		}
		setFireChangeEventOnSetValue(true);
		value = 0;
		propertyEditor = new NumberPropertyEditor();
	}
	
	protected void onRender(Element target, int index) {
		super.onRender(target, index);
	}
	
	protected void _onRender(Element target, int index){
		wrap = new El(DOM.createDiv());
	    wrap.dom.setPropertyString("hideFocus", "hideFocus");
	    wrap.dom.setClassName("x-form-check-wrap");
	   
	    El table = new El(DOM.createTable());
	    Element   body = DOM.createTBody();
	    table.appendChild(body);
	    for(int i=0;i<checknum;i++){
	    	input[i].setId(XDOM.getUniqueId());
		    El  tr = new El(DOM.createTR());
		    El  td = new El(DOM.createTD());
		    El  td2 = new El(DOM.createTD());
		    
		    body.appendChild(tr.dom);
		   
		    tr.appendChild(td2.dom);
		    tr.appendChild(td.dom);
		    El  boxLabelEl = new El(DOM.createLabel());
	        boxLabelEl.setStyleAttribute("position", "static");
	        boxLabelEl.setElementAttribute("for", input[i].getId());
	        boxLabelEl.setElementAttribute("htmlFor", input[i].getId());
	        boxLabelEl.dom.setClassName("x-form-cb-label");
	        boxLabelEl.update(this.title[i]);
	        td2.appendChild(input[i].dom);
	        td.appendChild(boxLabelEl.dom);
	     
	    }
	    wrap.dom.appendChild(table.dom);
	    setElement(wrap.dom, target, index);
	     
	}
	public void setValue(Number value) {

		Number oldValue = this.value;
		this.value = value;
		if (rendered) {
			Number v;// = value == null ? "" :
			// propertyEditor.getStringValue(value);
			try {
				int vtemp = (int)Double.parseDouble(value+"");
				v =value;
				
		
				for(int i = 0;i<checknum;i++)
				{
					((InputElement)input[i].dom.cast()).setChecked((vtemp==(i+1)));
//				 ((InputElement) input.dom.cast()).setDefaultChecked(value);
				}
				setRawValue(v+"");
				validate();

			} catch (NumberFormatException e) {
				setRawValue("0");
			}

		}
		if (((oldValue == null && value != null) || (oldValue != null && !oldValue
				.equals(value)))) {
			fireChangeEvent(oldValue, value);
		}

	}
	protected void onClick(ComponentEvent ce) {
		if (readOnly) {
			ce.stopEvent();
			return;
		}
		int value = 0;
		GWT.log(""+ce.getComponent());
		GWT.log(ce.getTarget().cast()+"");
		GWT.log(ce.getTargetEl()+"");
		
		((InputElement)ce.getTarget().cast()).setChecked(true);
		for(int i=0;i<this.checknum;i++)
		{
			
			if((input[i].dom) == ce.getTarget())
			{
				value = i+1;
				break;
			}

		}
/*		for(int i=0;i<this.checknum;i++)
		if(((InputElement)input[i].dom.cast()).isChecked())
		{
//			value =value | (1<<i);
			value = i;
			break;
		}*/
		if(value!=0)
		setValue(value);
		
	}
}
